package com.dearbinge.data.service.impl.im;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.im.UserContactTagRelationService;
import com.dearbinge.data.dao.UserMapper;
import com.dearbinge.data.dao.im.ImUserContactMapper;
import com.dearbinge.data.dao.im.ImUserContactTagRelationMapper;
import com.dearbinge.data.pojo.im.ImUserContact;
import com.dearbinge.data.pojo.im.ImUserContactTagRelation;
import com.dearbinge.data.pojo.im.vo.ImUserContactTagRelationVO;
import com.dearbinge.data.pojo.user.User;
import com.dearbinge.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 好友关系标签实现层
 *
 * @author wes
 * @date 2019/3/4
 */
@Service(value = "userContactTagRelationService")
public class UserContactTagRelationServiceImpl implements UserContactTagRelationService {

    @Autowired
    private ImUserContactTagRelationMapper imUserContactTagRelationMapper;
    @Autowired
    private ImUserContactMapper imUserContactMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 删除
     *
     * @param id id
     * @return 结果
     */
    @Override
    public Integer delete(Integer id) {
        return imUserContactTagRelationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新
     *
     * @param imUserContactTagRelation 用户
     * @return 结果
     */
    @Override
    public Integer update(ImUserContactTagRelation imUserContactTagRelation) {
        return imUserContactTagRelationMapper.update(imUserContactTagRelation);
    }

    /**
     * 插入
     *
     * @param imUserContactTagRelation 用户
     * @return 结果
     */
    @Override
    public Integer insert(ImUserContactTagRelation imUserContactTagRelation) {
        //插入关系表
        int insert = imUserContactTagRelationMapper.insert(imUserContactTagRelation);

        return insert;

    }

    /**
     * 根据用户id查询
     *
     * @param id id
     * @return 结果
     */
    @Override
    public ImUserContactTagRelation getById(Integer id) {
        return imUserContactTagRelationMapper.getByPrimaryKey(id);
    }

    /**
     * 根据标签ID查询标签组成员
     *
     * @param tagId   标签ID
     * @param current 当前页
     * @param size    每页条数
     * @return 分页结果
     */
    @Override
    public Page listByTagId(Integer tagId, Integer current, Integer size) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        Page<ImUserContactTagRelationVO> page = new Page<ImUserContactTagRelationVO>(current, size);
        List<ImUserContactTagRelationVO> userContact =  imUserContactTagRelationMapper.listByTagId(tagId,page);
        //插入头像 名字
        for (ImUserContactTagRelationVO imUserContactTagRelationVO : userContact) {
            ImUserContact imUserContact = imUserContactMapper.getByPrimaryKey(imUserContactTagRelationVO.getContactId());
            imUserContactTagRelationVO.setFriendName(imUserContact.getAlias());
            imUserContactTagRelationVO.setFriendId(imUserContact.getFriendId());
            User user = userMapper.getById(imUserContact.getFriendId());
            imUserContactTagRelationVO.setHeadImg(user.getHeadImg());
        }
        Integer count =  imUserContactTagRelationMapper.countByTagId(tagId);
        page.setTotal(count);
        page.setRecords(userContact);
        return page;
    }
}
